#include "base_desk.h"
#include "base_player.h"
#include "base_room.h"
#include "game_base_msg.h"
BaseDesk::BaseDesk(void)
{
	memset(m_chairPlayers, 0, sizeof(m_chairPlayers));
	m_isFriend = false;
}

BaseDesk::~BaseDesk(void)
{
}

void BaseDesk::onPlayerJoin( BasePlayer* player )
{

}

void BaseDesk::onPlayerLeave( BasePlayer* player )
{

}

void BaseDesk::onPlayerDisconnect( BasePlayer* player )
{

}

void BaseDesk::onPlayerData( BasePlayer* player, void* data, int len )
{

}

void BaseDesk::procPlayerJoin(BasePlayer* player, int deskId, int chairId) {
	player->m_desk = this;
	player->m_chairId = chairId;
	m_chairPlayers[chairId] = player;
	onPlayerJoin(player);

	BaseMsgS2CJoinDesk msg;
	msg.success = true;
	msg.chairId = chairId;
	msg.deskInfo.deskId = m_id;

	for (int i = 0; i < 4; i++) {
		if (m_chairPlayers[i]) {
			BaseMsgPlayerInfo playerInfo;
			m_chairPlayers[i]->getPlayerInfoMsg(&playerInfo);
			msg.deskInfo.playerNames.push_back(playerInfo);
			msg.deskInfo.playerChairIds.push_back(i);
		}
	}
	player->sendMessage(2, msg);
	
	BaseMsgDeskS2COtherPlayerJoinDesk otherJoinMsg;
	otherJoinMsg.chairId = chairId;
	otherJoinMsg.deskId = deskId;
	player->getPlayerInfoMsg(&otherJoinMsg.playerInfo);
	JMsgWriter writer;
	otherJoinMsg.encode(m_proto, &writer);
	brocast(2, 0, writer.getBuffer(), writer.getBufferLen(), player);
}

void BaseDesk::procPlayerLeave(BasePlayer* player) {
	onPlayerLeave(player);
	if (player->m_chairId >= 0 && player->m_chairId < 4 && m_chairPlayers[player->m_chairId] == player) {
		m_chairPlayers[player->m_chairId] = NULL;
	}

	player->m_desk = NULL;
	
	BaseMsgDeskS2COtherPlayerLeaveDesk otherLeaveMsg;
	otherLeaveMsg.chairId = player->m_chairId;
	otherLeaveMsg.userName = player->m_userName;

	JMsgWriter writer;
	otherLeaveMsg.encode(m_proto, &writer);
	brocast(2, 0, writer.getBuffer(), writer.getBufferLen(), player);

	if (!hasPlayerSitted() && m_isFriend) {		
		m_room->m_mapFriendDesks.erase(m_deskCode);
		m_deskCode = "";
		m_isFriend = false;
	}
}

bool BaseDesk::hasPlayerSitted() {
	for (int i = 0; i < 4; i++) {
		if (m_chairPlayers[i]) {
			return true;
		}
	}
	return false;
}

void BaseDesk::brocast(int mainCmd, int subCmd, void* data, int len, BasePlayer* except ) {
	for (int i = 0; i < 4; i++) {
		if (m_chairPlayers[i] && m_chairPlayers[i] != except) {
			m_chairPlayers[i]->sendData(mainCmd, subCmd, data, len);
		}
	}
}

void BaseDesk::brocastData(void* data, int len, BasePlayer* except) {
	this->brocast(2, 1, data, len, except);
}

void BaseDesk::tickPlayer(BasePlayer* player) {
	BaseMsgDeskS2COtherPlayerLeaveDesk msg;
	msg.userName = player->m_userName;
	msg.deskId = m_id;
	msg.chairId = player->m_chairId;

	JMsgWriter writer;
	msg.encode(m_proto, &writer);
	brocast(2, 0, writer.getBuffer(), writer.getBufferLen());

	this->onPlayerLeave(player);
	player->m_desk = NULL;
	player->m_chairId = -1;
}

int BaseDesk::getEmptyChairId() {
	for (int i = 0; i < 4; i++) {
		if (!m_chairPlayers[i]) {
			return i;
		}
	}
	return -1;
}